package com.sky.rabc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.rabc.dto.AddNewBackRoleDTO;
import com.sky.rabc.dto.UpdateBackRoleDTO;
import com.sky.rabc.enums.ServiceCode;
import com.sky.rabc.exception.ServiceException;
import com.sky.rabc.mapper.BackRoleAndPermissionMapper;
import com.sky.rabc.mapper.BackRoleMapper;
import com.sky.rabc.mapper.BackUserAndRoleMapper;
import com.sky.rabc.service.BackRoleService;
import com.sky.rabc.utils.PageData;
import com.sky.rabc.utils.PageInfoToPageDataConverter;
import com.sky.rabc.vo.BackRoleListItemVO;
import com.sky.rabc.vo.BackRoleVO;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BackRoleServiceImpl implements BackRoleService {
    private final BackRoleMapper backRoleMapper;
    private final BackUserAndRoleMapper backUserAndRoleMapper;
    private final BackRoleAndPermissionMapper backRoleAndPermissionMapper;

    public BackRoleServiceImpl(BackRoleMapper backRoleMapper, BackUserAndRoleMapper backUserAndRoleMapper, BackRoleAndPermissionMapper backRoleAndPermissionMapper) {
        this.backRoleMapper = backRoleMapper;
        this.backUserAndRoleMapper = backUserAndRoleMapper;
        this.backRoleAndPermissionMapper = backRoleAndPermissionMapper;
    }

    @Override
    public void addNew(AddNewBackRoleDTO addNewBackRoleDTO) {
        int addResult = backRoleMapper.addNew(addNewBackRoleDTO);
        if (addResult <= 0) {
            throw new ServiceException(ServiceCode.ERROR_INSERT, "新增角色失败，稍后再试");
        }
    }

    @Override
    public void update(UpdateBackRoleDTO updateBackUserDTO) {
        //判断role是否存在
        validateRole(updateBackUserDTO.getId());
        int updateResult = backRoleMapper.update(updateBackUserDTO);
        if (updateResult <= 0) {
            throw new ServiceException(ServiceCode.ERROR_UPDATE, "数据修改失败，稍后再试");
        }
    }

    @Override
    public void delRoleById(Long id) {
        //判断角色是否存在
        validateRole(id);
        //查询用户角色关联表中是否有关联，有则删除关联
        validateUserAndRole(id);
        //查询角色权限关联表中是否有关联，有则删除关联
        validateRoleAndPermission(id);
        int delResult = backRoleMapper.delRoleById(id);
        if (delResult <= 0) {
            throw new ServiceException(ServiceCode.ERROR_DELETE, "删除失败，请稍后重试");
        }
    }

    @Override
    public BackRoleVO getRoleById(Long id) {
        BackRoleVO backRoleVO = backRoleMapper.getRoleById(id);
        if (backRoleVO == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "数据不存在");
        }
        return backRoleVO;
    }

    @Override
    public PageData<BackRoleListItemVO> list(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<BackRoleListItemVO> list = backRoleMapper.list();
        PageInfo<BackRoleListItemVO> pageInfo = new PageInfo<>(list);
        PageData<BackRoleListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    //判断角色是否存在
    private void validateRole(Long id) {
        int countResult = backRoleMapper.countRoleById(id);
        if (countResult <= 0) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "数据不存在");
        }
    }

    //查询用户角色关联表中是否有关联，有则删除关联
    private void validateUserAndRole(Long roleId) {

        int counted = backUserAndRoleMapper.countUserAndRoleByRoleId(roleId);
        if (counted > 0) {
            int delResult = backUserAndRoleMapper.del(roleId);
            if (delResult <= 0) {
                throw new ServiceException(ServiceCode.ERROR_DELETE, "删除失败，请稍后重试");
            }
        }
    }

    //查询角色权限关联表中是否有关联，有则删除关联
    private void validateRoleAndPermission(Long roleId) {
        int countedByRoleId = backRoleAndPermissionMapper.countByRoleId(roleId);
        if (countedByRoleId > 0) {
            int delResult = backRoleAndPermissionMapper.delByRoleId(roleId);
            if (delResult <= 0) {
                throw new ServiceException(ServiceCode.ERROR_DELETE, "删除失败，请稍后重试");
            }
        }
    }
}
